// pages/integral/index.js
import { fetchScoreshopIndex, fetchScoreshopSptype } from '~/request/api';

Page({
  /**
   * 页面的初始数据
   */
  data: {
    catgoryList: [],
    indexInfo: {
      recommend_list: [], // 推荐
      hot_list: [], // 热门
      score: 0
    },
    pageLoading: false,
  },

  // 跳转商品详情
  goodListClickHandle(e) {
    const { goods } = e.detail;
    wx.navigateTo({
      url: `/pages/goods/details/index?goods_id=${goods.id}&type=integral`,
    });
  },

  async shopIndex() {
    try {
      const res = await fetchScoreshopIndex();
      const recommend_list = res.recommend_list.map((item) => ({ ...item, market_price: '0' }));
      const hot_list = res.hot_list.map((item) => ({ ...item, market_price: '0' }));
      this.setData({
        indexInfo: { ...res, hot_list, recommend_list },
        pageLoading: false,
      });
    } catch (err) {
      console.log(err);
    }
  },

  // 获取类目
  async shopSptype() {
    try {
      const res = await fetchScoreshopSptype();
      this.setData({ catgoryList: res?.children?.splice(0, 5) });
    } catch (error) {
      console.log(error);
    }
  },

  loadHomePage() {
    wx.stopPullDownRefresh();

    this.setData({
      pageLoading: true,
    });
    this.shopIndex();
  },

  // 点击类目回调
  handleTab(e) {
    const { id } = e.currentTarget;
    wx.navigateTo({
      url: `/pages/goods/category/index?tab=${id}&type=integral`,
    });
  },

  init() {
    this.loadHomePage();
    this.shopSptype();
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.init();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() { },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getTabBar().init();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() { },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() { },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.init();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() { },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() { },
});
